package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.PasswordInputView;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 实现现金币提现
 */

public class WithdrawDespositActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.withdraw_iv_head)
    ImageView withdraw_iv_head;
    @BindView(R.id.withdraw_tv_account)
    TextView withdraw_tv_account;
    @BindView(R.id.withdraw_tv_num)
    TextView withdraw_tv_num;
    @BindView(R.id.withdraw_et_money)
    EditText withdraw_et_money;
    private Dialog hintDialog = null;
    private double strMon;
    private double mMoney;
    private int type;
    private String trueName;
    private String bankName;
    private String bankInfo;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("现金币提现");
        strMon = Double.valueOf(getIntent().getStringExtra("strMoney")) / 1.03;
        mMoney = Math.floor(strMon);
        withdraw_et_money.setHint("当前可提现" + mMoney);

    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_withdraw_desposit;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    /**
     * 选择提现账号
     */
    @OnClick(R.id.withdraw_ll_account)
    public void onClickLLAccount() {
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), WithdrawAccountActivity.class);
        startActivityForResult(intent, 118);
    }

    /**
     * 提现提交
     */
    @OnClick(R.id.withdraw_tv_money_commit)
    public void onClickMoneyCommit() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(withdraw_tv_num.getText().toString().trim())) {
            ToastUitl.showShort("提现账户不能为空～");
            return;
        } else if (TextUtils.isEmpty(withdraw_et_money.getText().toString().trim())) {
            ToastUitl.showShort("提现金额不能为空～");
            return;
        } else if (Double.valueOf(withdraw_et_money.getText().toString().trim()) > strMon) {
//            ToastUitl.showShort("提现金额不能大于可提现金额～");
            withdraw_et_money.setText("" + mMoney);
            return;
        } else if (Double.valueOf(withdraw_et_money.getText().toString().trim()) < 1) {
            ToastUitl.showShort("提现金额必须大于0～");
            return;
        }
        hintDialog = new Dialog(WithdrawDespositActivity.this, R.style.dialog_style1);
        LinearLayout layout;
        LayoutInflater inflater = (LayoutInflater) WithdrawDespositActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = (LinearLayout) inflater.inflate(R.layout.dialog_hint,
                null);
        final PasswordInputView tv_hint = (PasswordInputView) layout
                .findViewById(R.id.again_paypswd_pet);
        layout.findViewById(R.id.dialog_btn_cancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        hintDialog.dismiss();
                    }
                });
        layout.findViewById(R.id.dialog_btn_ensure).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        initRequestPayPassword(tv_hint.getText().toString().trim());
                        hintDialog.dismiss();
                    }
                });
        hintDialog.setContentView(layout);
        hintDialog.show();


    }

    /**
     * 输入平台支付密码
     *
     * @param password
     */
    private void initRequestPayPassword(String password) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestPayPassword(UserUtils.getUserToken(),
                        password, new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        if (type == 0) {
                                            initRequest();
                                        } else if(type == 1){
                                            initRequestBank();
                                        }

                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                            }
                        });

    }

    /**
     * 支付宝提现请求
     */
    private void initRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(withdraw_tv_num.getText().toString().trim())) {
            ToastUitl.showShort("提现账户不能为空～");
            return;
        } else if (TextUtils.isEmpty(withdraw_et_money.getText().toString().trim())) {
            ToastUitl.showShort("提现金额不能为空～");
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestElecMoney(UserUtils.getUserToken()
                        , Double.valueOf(withdraw_et_money.getText().toString().trim()),
                        "1", withdraw_tv_num.getText().toString(),
                        withdraw_tv_account.getText().toString(),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(WithdrawDespositActivity.this);

                            }

                            @Override
                            public void onCompleted() {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                LoadingDialog.cancelDialogForLoading();
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort(baseBean.getError_message());
                                        finish();
                                    } else if (baseBean.getError_code() == -1) {
                                        ToastUitl.showShort(baseBean.getError_message());

                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else if (baseBean.getError_code() == 1) {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });

    }

    /**
     * 银行卡提现请求
     */
    private void initRequestBank() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(withdraw_tv_num.getText().toString().trim())) {
            ToastUitl.showShort("提现账户不能为空～");
            return;
        } else if (TextUtils.isEmpty(withdraw_et_money.getText().toString().trim())) {
            ToastUitl.showShort("提现金额不能为空～");
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestBankElecMoney(UserUtils.getUserToken()
                        , Double.valueOf(withdraw_et_money.getText().toString().trim()),
                        "3", withdraw_tv_num.getText().toString(),
                        trueName,
                        bankName,
                        bankInfo,
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(WithdrawDespositActivity.this);

                            }

                            @Override
                            public void onCompleted() {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                LoadingDialog.cancelDialogForLoading();
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort(baseBean.getError_message());
                                        finish();
                                    } else if (baseBean.getError_code() == -1) {
                                        ToastUitl.showShort(baseBean.getError_message());

                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else if (baseBean.getError_code() == 1) {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 118 && resultCode == 180) {
            withdraw_iv_head.setVisibility(View.VISIBLE);
            withdraw_tv_account.setText(data.getStringExtra("username"));
            withdraw_tv_num.setVisibility(View.VISIBLE);
            withdraw_tv_num.setText(data.getStringExtra("useraccount"));
            Picasso.get().load(R.mipmap.zhifubaox36).into(withdraw_iv_head);
            type = data.getIntExtra("type", 0);
        } else if (requestCode == 118 && resultCode == 181) {
            withdraw_iv_head.setVisibility(View.VISIBLE);
            Picasso.get().load(BaseContacts.BASE_URL +
                    data.getStringExtra("banklogo")).into(withdraw_iv_head);
            withdraw_tv_account.setText(data.getStringExtra("bankname") +
                    " (" + data.getStringExtra("username") + ")");
            trueName = data.getStringExtra("username");
            bankName = data.getStringExtra("bankname");
            bankInfo = data.getStringExtra("bankInfo");
            withdraw_tv_num.setVisibility(View.VISIBLE);
            withdraw_tv_num.setText(data.getStringExtra("useraccount"));
            type = data.getIntExtra("type", 0);
        }
    }
}
